package example.jaxws;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 28.11.2008
 * Time: 04:48:13
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class NewIDGener {
    public static String COUNT_FILE_PATH = "D:\\glassfish\\domains\\domain1\\docroot\\conf\\count.txt";
    private static Properties idProperties = new Properties();

    public static int newId(String idLabel) {
        loadIdList();

        int id;
        if (!idProperties.containsKey(idLabel)) {
            idProperties.setProperty(idLabel, "1");
            id = 1;
        } else {
            id = Integer.valueOf(idProperties.getProperty(idLabel));
            idProperties.setProperty(idLabel, String.valueOf(++id));
        }

        saveIdList();
        return id;
    }

    private static void loadIdList() {
        try {
            idProperties.load(new FileInputStream(COUNT_FILE_PATH));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error By loading id" + e);
        }
    }

    private static void saveIdList() {
        try {
            idProperties.store(new FileOutputStream(COUNT_FILE_PATH), "Auto Saved ID counter");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error By save id" + e);
        }
    }

}
